﻿/*
 * 描 述：文件管理
 */
var acceptClick;

var bootstrap = function ($, learun) {
    "use strict";

    var fileInfo = {};
    var files = [];
    var page = {
        uploader: null,
        init: function () {
            if (!WebUploader.Uploader.support()) {
                alert('Web Uploader 不支持您的浏览器！如果你使用的是IE浏览器，请尝试升级 flash 播放器');
                throw new Error('WebUploader does not support the browser you are using.');
            }

            page.uploader = WebUploader.create({
                auto: true,
                swf: config.webportal_server + '/assets/plugins/webuploader/Uploader.swf',
                // 文件接收服务端。
                //server: config.base_server() + 'common/upload?access_token=' + config.getToken().access_token,
                server: "http://127.0.0.1:11111/invoice-ocr",
                // 选择文件的按钮。可选。
                // 内部根据当前运行是创建，可能是input元素，也可能是flash.
                pick: '#lr_add_file_btn',
                dnd: '#lr_form_file_queue',
                paste: 'document.body',
                accept: {
                    extensions: "jpg,jpeg,png,gif,bmp"
                },
                multiple: false,
                // 上传参数
                formData: {
                    __RequestVerificationToken: $.lrToken
                }
            });

            page.uploader.on('fileQueued', page.fileQueued);
            page.uploader.on('uploadStart', page.uploadStart);
            page.uploader.on('uploadBeforeSend', page.uploadBeforeSend);
            page.uploader.on('uploadProgress', page.uploadProgress);
            page.uploader.on('uploadSuccess', page.uploadSuccess);
            page.uploader.on('uploadError', page.uploadError);
            page.uploader.on('uploadComplete', page.uploadComplete);
            page.uploader.on('error', page.error);

        },
        fileQueued: function (file) {// 文件加载到队列
            fileInfo[file.id] = {name: file.name};
            // 创建缩略图
            page.uploader.makeThumb(file, function (error, src) {
                if (error) {
                    $img.replaceWith('<span>不能预览</span>');
                    return;
                }
                $("#img_invoice").attr('src', src);
            }, 720, 430);

            $("#qr-scanner").show();
            $("#lodding").show();
        },
        uploadStart: function (file) {

        },
        uploadBeforeSend: function (object, data, headers) {
        },
        uploadProgress: function (file, percentage) {
        },
        uploadSuccess: function (file, res) {
            //alert(JSON.stringify(res));
            $("#ocr_result").empty();
            var str = "<tr><td>发票代码:</td><td>" + res.data.code + "</td></tr>";
            str += "<tr><td>发票号码:</td><td>" + res.data.number + "</td></tr>";
            str += "<tr><td>开票日期:</td><td>" + res.data.date + "</td></tr>";
            str += "<tr><td>税后价格:</td><td>" + res.data.amount + "</td></tr>";
            $("#ocr_result").append(str);
        },
        uploadError: function (file, code) {
        },
        uploadComplete: function (file) {
            $("#qr-scanner").hide();
            $("#lodding").hide();
        },
        error: function (type) {
            switch (type) {
                case 'Q_TYPE_DENIED':
                    learun.alert.error('当前文件类型不允许上传');
                    break;
            }
        }
    };
    // 保存数据
    acceptClick = function (callBack) {
        callBack(files);
        return true;
    };

    page.init();
}